﻿Public Class DRUGTIME

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub DRUGTIME_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridMASDRGTIME()

        DesignForm(Me)
        DesignGrid(MASDRGTIMEGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridMASDRGTIME()
        Dim objSelectDataMASDRGTIME As New masdrgtime
        Dim MASDRGTIME As DataTable
        MASDRGTIME = connect.GetTable(objSelectDataMASDRGTIME.selectMasdrgtime_)
        MASDRGTIMEGrid.PrimaryGrid.DataSource = MASDRGTIME
    End Sub

    Private Sub MASDRGTIMEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASDRGTIMEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            TIMEIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            TIMEIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสระบุเวลา").Value() IsNot DBNull.Value Then
            DTCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสระบุเวลา").Value()
        Else
            DTCODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา").Value() IsNot DBNull.Value Then
            DTNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วิธีการใช้ยา").Value()
        Else
            DTNAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวนครั้งที่ใช้ยา").Value() IsNot DBNull.Value Then
            QTYTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวนครั้งที่ใช้ยา").Value()
        Else
            QTYTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masdrgtimeStatusChk.Checked = True
        Else
            masdrgtimeStatusChk.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masdrgtime SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `TIMEID` = '" & TIMEIDTxt.Text & "' ;"

                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        addMasdrgtime.Enabled = False
    End Sub

    Private Sub addMasdrgtime_Click(sender As Object, e As EventArgs) Handles addMasdrgtime.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If DTCODETxt.Text.ToString.Length > 0 And DTNAMETxt.Text.ToString.Length > 0 And QTYTxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New masdrgtime
                    objInsertData.DTCODE_ = DTCODETxt.Text
                    objInsertData.DTNAME_ = DTNAMETxt.Text
                    objInsertData.QTY_ = QTYTxt.Text
                    objInsertData.STATUS_ = masdrgtimeStatusChk.Checked
                    objInsertData.insertMasdrgtimeData()

                    SelectDatagridMASDRGTIME()

                    TIMEIDTxt.Text = ""
                    DTCODETxt.Text = ""
                    DTNAMETxt.Text = ""
                    QTYTxt.Text = ""
                    masdrgtimeStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasdrgtime_Click(sender As Object, e As EventArgs) Handles editMasdrgtime.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If TIMEIDTxt.Text.ToString.Length > 0 And DTCODETxt.Text.ToString.Length > 0 And DTNAMETxt.Text.ToString.Length > 0 And QTYTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลการใช้ยาดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New masdrgtime
                        objEditData.TIMEID_ = TIMEIDTxt.Text
                        objEditData.DTCODE_ = DTCODETxt.Text
                        objEditData.DTNAME_ = DTNAMETxt.Text
                        objEditData.QTY_ = QTYTxt.Text
                        objEditData.STATUS_ = masdrgtimeStatusChk.Checked
                        objEditData.editMasdrgtimeData()

                        SelectDatagridMASDRGTIME()

                        TIMEIDTxt.Text = ""
                        DTCODETxt.Text = ""
                        DTNAMETxt.Text = ""
                        QTYTxt.Text = ""
                        masdrgtimeStatusChk.Checked = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
                addMasdrgtime.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("masdrgtime")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("masdrgtime")
        nextform.ShowDialog()
    End Sub
End Class